<?php

namespace Biz\Transformers\Site;

use Biz\Dictionaries\Order\OrderStatusDict;
use Lumen\Http\Resources\Json\Resource;
use Biz\Constants\Site\SiteOrderConstants;

/**
 * OrderTransformer.
 *
 * @license [http://www.85do.com] [杭州永奥网络科技有限公司]
 * @copyright Copyright (c) 2018-2026 Hangzhou Yongao Technology Co., Ltd. All rights reserved.
 */
class OrderTransformer extends Resource
{
    /**
     * Transform the resource into an array.
     *
     * @param \Illuminate\Http\Request $request
     *
     * @return array
     */
    public function toArray($request)
    {
        return [
            'uid'          => $this->uid,
            'type'         => $this->type,
            'type_name'    => SiteOrderConstants::$TypeNames[$this->type],
            'order_no'     => $this->order_no,
            'currency'     => $this->currency,
            'total_amount' => money_format('%i', $this->total_amount),
            'status'       => $this->status,
            'status_name'  => OrderStatusDict::get($this->status),
            'created_at'   => $this->created_at->getTimestamp(),
            $this->mergeWhen($this->relationLoaded('hotel_reservation'), [
                'hotel_reservation' => $this->getOrderHotelReservationTransformer(),
            ]),
            $this->mergeWhen($this->relationLoaded('registration_fee'), [
                'registration_fee' => $this->getOrderRegistrationFeeTransformer(),
            ]),
        ];
    }

    /**
     * Get app resource.
     *
     * @return OrderHotelReservationTransformer|null
     */
    public function getOrderHotelReservationTransformer()
    {
        if ($this->relationLoaded('hotel_reservation')) {
            return new OrderHotelReservationTransformer($this->hotel_reservation);
        }

        return null;
    }

    /**
     * Get app resource.
     *
     * @return SitePayEaseResource|null
     */
    public function getOrderRegistrationFeeTransformer()
    {
        if ($this->relationLoaded('registration_fee')) {
            return new OrderRegistrationFeeTransformer($this->registration_fee);
        }

        return null;
    }
}
